<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Push_manage extends MY_Controller
{
	private $datas;
	private $coins;

	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
		$this->coins = $this->d->rows(array('table' => 'coin', 'where' => 'status=1'));
	}

	public function index()
	{
		$flag = intval($this->input->get('flag'));
		$d['flag'] = $flag;
		$type = intval($this->input->get('type'));
		$d['type'] = $type;
		$d['coins'] = $this->coins;

		if ($flag == 0) {
			$rows = $this->result_list(array('table' => 'push', 'where' => 'user_id=' . $this->user['id'], 'order' => 'add_time desc', 'url' => 'pash_manage/index', 'per_page' => 10));
		}
		else {
			$rows = $this->result_list(array('table' => 'push', 'where' => 'to_uid=' . $this->user['id'] . ' and status=0', 'order' => 'add_time desc', 'url' => 'pash_manage/index', 'per_page' => 10));
		}

		foreach ($rows['list'] as $k => $v) {
			$rows['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
		}

		$d['list'] = $rows['list'];
		$d['page'] = $rows['page'];
		$this->show(array('header', 'user_left', 'push_manage', 'footer'), $d);
	}

	public function insert()
	{
		if ($this->cfg['is_push'] == 0) {
			ajax_return(1, '系统关闭中');
		}

		$paypwd = $this->input->post('paypwd');
		if (!$paypwd || !$this->pwd->verify($paypwd, $this->user['paypwd'])) {
			ajax_return(1, '安全密码错误');
		}

		$type = intval($this->input->post('type'));

		if (!in_array($type, array(0, 1))) {
			ajax_return(1, '找不到交易类型');
		}

		$coin_id = intval($this->input->post('coin_id'));

		if (!$coin_id) {
			ajax_return(1, '找不到交易币种');
		}

		$coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $coin_id));

		if (!$coin) {
			ajax_return(1, '找不到投资币种');
		}

		$amount = num($this->input->post('amount'));

		if (!$amount) {
			ajax_return(1, '数量过低');
		}

		$price = num($this->input->post('price'));

		if (!$price) {
			ajax_return(1, '价格过低');
		}

		$total = num($this->input->post('total'));

		if (!$total) {
			ajax_return(1, '总额过低');
		}

		if ($type == 1) {
			$to_uid = $this->input->post('to_uid');

			if (!$to_uid) {
				ajax_return(1, '找不到目标用户UID');
			}
		}

		$price_coin_id = intval($this->input->post('price_coin_id'));

		if (!$price_coin_id) {
			ajax_return(1, '找不到支付币种');
		}

		$price_coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $price_coin_id));

		if (!$price_coin) {
			ajax_return(1, '找不到支付币种');
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write, t_push write');

		if ($type == 1) {
			$xnb = $this->db->where('user_id', $this->user['id'])->where('coin_id', $coin_id)->get('xnb')->row_array();

			if ($xnb['num'] < $amount) {
				$this->db->query('rollback');
				$this->db->query('unlock tables');
				ajax_return(1, '余额不足');
			}

			$rs1 = $this->db->where('id', $xnb['id'])->set('num', 'num-' . $amount, false)->set('num_frozen', 'num_frozen+' . $amount, false)->update('xnb');
		}
		else {
			$xnb = $this->db->where('user_id', $this->user['id'])->where('coin_id', $price_coin_id)->get('xnb')->row_array();

			if ($xnb['num'] < $total) {
				$this->db->query('rollback');
				$this->db->query('unlock tables');
				ajax_return(1, '余额不足');
			}

			$rs1 = $this->db->where('id', $xnb['id'])->set('num', 'num-' . $total, false)->set('num_frozen', 'num_frozen+' . $total, false)->update('xnb');
		}

		$data['coin_id'] = $coin_id;
		$data['amount'] = $amount;
		$data['price'] = $price;
		$data['total'] = $total;
		$data['to_uid'] = $to_uid;
		$data['price_coin_id'] = $price_coin_id;
		$data['add_time'] = time();
		$data['user_id'] = $this->user['id'];
		$data['type'] = $type;
		if ($rs1 && $this->db->insert('push', $data)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '订单已提交，请等待撮合', site_url('push_manage'));
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '数据验证失败，请重新填写');
		}
	}

	public function trade()
	{
		if ($this->cfg['is_push'] == 0) {
			ajax_return(1, '系统关闭中');
		}

		$this->verify_user_ajax();
		$id = intval($this->input->post('id'));

		if (!$id) {
			ajax_return(1, '找不到记录');
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write, t_push write, t_deal write, t_coin write');
		$push = $this->d->find(array('table' => 'push', 'where' => 'id=' . $id . ' and status=0'));

		if (!$push) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到交易对象');
		}

		$coin_id = $push['coin_id'];

		if (!$coin_id) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到交易币种');
		}

		$coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $coin_id));

		if (!$coin) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到投资币种');
		}

		$price_coin_id = $push['price_coin_id'];

		if (!$price_coin_id) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到支付币种');
		}

		$price_coin = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $price_coin_id));

		if (!$price_coin) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到支付币种');
		}

		$price = num($push['price']);

		if (!$price) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '价格过低');
		}

		$amount = num($push['amount']);

		if (!$amount) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '数量过低');
		}

		$total = num($push['price'] * $amount);

		if (!$total) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '总额过低');
		}

		$xnb = $this->d->field(array('field' => 'num', 'table' => 'xnb', 'where' => 'user_id=' . $this->user['id'] . ' and coin_id=' . $price_coin_id));

		if ($xnb < $total) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '余额不足');
		}

		$rs[] = $this->db->set('num', 'num-' . $total, false)->where('user_id', $this->user['id'])->where('coin_id', $price_coin_id)->update('xnb');
		$rs[] = $this->db->set('num', 'num+' . $amount, false)->where('user_id', $this->user['id'])->where('coin_id', $coin_id)->update('xnb');
		$rs[] = $this->db->set('num', 'num+' . $total, false)->where('user_id', $push['user_id'])->where('coin_id', $price_coin_id)->update('xnb');
		$rs[] = $this->db->set('num', 'num_frozen-' . $amount, false)->where('user_id', $push['user_id'])->where('coin_id', $coin_id)->update('xnb');
		$data = array('push_id' => $id, 'buy_user_id' => $this->user['id'], 'sell_user_id' => $push['user_id'], 'price' => $price, 'amount' => $amount, 'total' => $total, 'price_coin_id' => $price_coin_id, 'coin_id' => $coin_id, 'add_time' => time());
		$rs[] = $this->db->insert('deal', $data);
		$rs[] = $this->db->set('deal', 'deal+' . $amount, false)->where('id', $id)->update('push');
		$rs[] = $this->db->set('status', 1)->where('id', $id)->where('deal>=amount')->update('push');

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '提交成功');
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '提交失败');
		}
	}

	public function deal_log()
	{
		$flag = intval($this->input->get('flag'));

		if ($flag == 0) {
			$d = $this->result_list(array('table' => 'deal', 'where' => 'buy_user_id=' . $this->user['id'], 'order' => 'add_time desc', 'url' => 'pash_manage/deal_log', 'per_page' => 10));
		}
		else {
			$d = $this->result_list(array('table' => 'deal', 'where' => 'sell_user_id=' . $this->user['id'], 'order' => 'add_time desc', 'url' => 'pash_manage/deal_log', 'per_page' => 10));
		}

		foreach ($d['list'] as $k => $v) {
			$d['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
			$d['list'][$k]['push'] = $this->d->find(array('table' => 'push', 'where' => 'id=' . $v['push_id']));
		}

		$d['flag'] = $flag;
		$this->show(array('header', 'user_left', 'push_log', 'footer'), $d);
	}
}

?>
